
global root_dir = "`1'"
include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A4b_firmpatent_descriptives.log, replace name(tabl)

capture noi {

* v6
* Descriptive statistics for ﬁrms in our baseline regression

texdoc init ${tab_dir}/appendix/Table_A4b_firmpatent_descriptives.tex, replace
texdoc stlog, nolog

clear all
global ttt "bia"
global depvar "auto95"
global weight_window "_from1970"
global tf "_tfacit1"

* ---------------------- * 
* a) count patents
* ---------------------- * 

use BvD lse_id year auto95_bia pauto90_bia using ${final_dir}/regression_dataset${weight_window}${tf}.dta, clear
keep if year >= 1995+2 & year <= 2009+2

* get regression samples for baseline + placebo specification
foreach regsample in auto95 ppauto90 {
		mmerge BvD using ${final_dir}/bvd_list_regfirms_`regsample'.dta, unmatched(master)
		if ("`regsample'" == "auto95") local s 1 
		if ("`regsample'" == "ppauto90") local s 2
		gen in_sample_`s' = (_m == 3)
		drop _m
}
* count total patents
foreach tech in auto95 pauto90 { 
	bys lse_id : egen _total_`tech'_${ttt} = sum(`tech'_${ttt})
	bys lse_id : egen total_`tech'_${ttt} = max(_total_`tech'_${ttt})
	drop _total_`tech'_${ttt}
}
* Store patent summary statistics
foreach s in 1 2 {
	foreach tech in auto95 pauto90 { 
		qui su `tech'_${ttt} if in_sample_`s', detail
		foreach stat in mean sd { 
			sleep 1500
			texdoc local y`s'_`tech'_`stat' : display %9.0f r(`stat')
		}
		foreach stat in p50 p75 p90 p95 p99 { 
			sleep 1500
			texdoc local y`s'_`tech'_`stat' : display %9.0f r(`stat')
		}
		su total_`tech'_${ttt} if in_sample_`s', detail
		foreach stat in mean sd  {
			sleep 1500
			texdoc local t`s'_`tech'_`stat' : display %9.0f r(`stat')
		}
		foreach stat in p50 p75 p90 p95 p99 {
			sleep 1500
			texdoc local t`s'_`tech'_`stat' : display %9.0f r(`stat')
		}
	}
}

* ---------------------- * 
* e) Firm counts
* ---------------------- *

preserve
qui keep BvD in_sample_*
qui duplicates drop
foreach s in 1 2 {
	count if in_sample_`s'
	sleep 1500
	texdoc local firms`s' : display %9.0f r(N)
}
restore, preserve



* ---------------------- * 
* b) Avg 5-y citations 
* ---------------------- * 

* Load average citations (5yrs_norm; yearly and total), store average citations
use ${final_dir}/sample_descriptives.dta if in_period == 1 & uniq_fam == 1 & bia == 1, clear
keep BvD docdb_family_id cit_5yrs auto95 pauto90 pauto90_rm6

foreach regsample in auto95 ppauto90 {
		mmerge BvD using ${final_dir}/bvd_list_regfirms_`regsample'.dta, unmatched(master)
		if ("`regsample'" == "auto95") local s 1 
		if ("`regsample'" == "ppauto90") local s 2
		gen in_sample_`s' = (_m == 3)
		drop _m
}
foreach s in 1 2 {
	foreach tech in auto95 pauto90 pauto90_rm6 { 
		di "Sample `s' `tech'"
		su cit_5yrs if in_sample_`s' & `tech' == 1
		sleep 1500
		texdoc local avgcit`s'_`tech' : display %9.1f r(mean)
	}
}

* ---------------------- * 
* Output table
* ---------------------- * 

texdoc local notes "This table presents summary statistics for the firms' patenting activity. Columns 1 and 2 show statistics for the baseline regression sample. Columns 3 and 4 describe the restricted sample in which we include non-automation machinery (pauto90) patents. Average citations are calculated as the average number of citations received by a patent within 5 years after the application. The firms are the non-domestic firms that patent at least once before 1995 and during the sample period 1997-2011."

texdoc stlog close
include ${code_dir}/config/texdoctools.do
tex `texpreamble'

/*tex
\begin{tabular}{@{}l*{4}S[table-format=4.0]}
\toprule
Sample & \multicolumn{2}{c}{Baseline} & \multicolumn{2}{c}{Restricted} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& \multicolumn{2}{c}{Auto95} & {Auto95} & {Pauto90} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5}
& {(1)} & {(2)} & {(3)} & {(4)} \\

\noalign{\vskip 0.4mm}
\midrule 
\noalign{\vskip 0.4mm}
Number of patents \\
\noalign{\vskip -0.8mm}
& {Yearly} & {1997-2011} & {1997-2011} & {1997-2011}\\
\cmidrule(lr){2-2}\cmidrule(lr){3-3}\cmidrule(lr){4-4}\cmidrule(lr){5-5} 
\noalign{\vskip -0.8mm}
\; Mean & `y1_auto95_mean' & `t1_auto95_mean' & `t2_auto95_mean' & `t2_pauto90_mean' \\
\; SD & `y1_auto95_sd' & `t1_auto95_sd' & `t2_auto95_sd' & `t2_pauto90_sd' \\
\; P50 & `y1_auto95_p50' & `t1_auto95_p50' & `t2_auto95_p50' & `t2_pauto90_p50' \\
\; P75 & `y1_auto95_p75' & `t1_auto95_p75' & `t2_auto95_p75' & `t2_pauto90_p75' \\
\; P90 & `y1_auto95_p90' & `t1_auto95_p90' & `t2_auto95_p90' & `t2_pauto90_p90' \\
\; P95 & `y1_auto95_p95' & `t1_auto95_p95' & `t2_auto95_p95' & `t2_pauto90_p95' \\
\; P99 & `y1_auto95_p99' & `t1_auto95_p99' & `t2_auto95_p99' & `t2_pauto90_p99' \\

\noalign{\vskip 0.4mm}
\midrule 
\noalign{\vskip 0.4mm}
\makecell[cl]{Average citations \\ received in 5 years} & \multicolumn{2}{c}{`avgcit1_auto95'} & `avgcit2_auto95' & `avgcit2_pauto90' \\
\noalign{\vskip 0.4mm}
\midrule 
\noalign{\vskip 0.4mm}
Number of firms & \multicolumn{2}{c}{`firms1'} & \multicolumn{2}{c}{`firms2'} \\

\bottomrule
\end{tabular}
tex*/
tex `texpostamble'
texdoc close


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl